package su.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import su.domain.Dep;
import su.domain.Emp;
import su.domain.Msg;
import su.service.DepService;
import su.service.EmpService;

import javax.validation.Valid;
import java.util.*;

@Controller()
//@RequestMapping(path = "/emp")
public class EmpController {

    @Autowired
    private EmpService empService;
    @Autowired
    private DepService depService;

//    /**
//     * 分页、多表查询数据封装
//     * @param pn
//     * @param model
//     * @return
//     */
//    @RequestMapping(path = "/emps")
//    public String list(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
//        PageHelper.startPage(pn, 8);
//        List<Emp> emps = empService.listwithdep(pn,8);
//        PageInfo pageInfo = new PageInfo(emps, 8);
//        model.addAttribute("pageemps", pageInfo);
//        return "list";
//    }

//
//    /**
//     * 直接返回分页的json数据
//     * 返回体注解 需要导入jackson依赖
//     *
//     * @param pn
//     * @return
//     */
//    @RequestMapping(path = "/emps")
//    @ResponseBody
//    public PageInfo getempswithjson(@RequestParam(value = "pn", defaultValue = "1") Integer pn) {
//        PageHelper.startPage(pn, 8);
//        List<Emp> emps = empService.listwithdep(pn, 8);
//        PageInfo pageInfo = new PageInfo(emps, 8);
//        return pageInfo;
//    }


    /**
     * 直接返回分页的json数据
     * 返回体注解 需要导入jackson依赖
     *
     * @param pn
     * @return
     */
    @RequestMapping(path = "/emps")
    @ResponseBody
    public Msg getempswithjson(@RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 8);
        List<Emp> emps = empService.listwithdep(pn, 8);
        PageInfo pageInfo = new PageInfo(emps, 8);
        return Msg.success().add("pageemps", pageInfo);
    }

    /**
     * 获取所有部门信息
     *
     * @return
     */
    @RequestMapping(path = "/getdep")
    @ResponseBody
    public Msg getdep() {
        List<Dep> deps = depService.findAll();
        return Msg.success().add("dep", deps);
    }


    /**
     * 保存员工
     * jsr303校验
     *
     * @return
     */
    @RequestMapping(value = "/savedep", method = RequestMethod.POST)
    @ResponseBody
    public Msg saveEmp(@Valid Emp emp, BindingResult result) {
        Map<String, Object> errormap = new HashMap<>();
        if (result.hasErrors()) {
            List<FieldError> fieldErrors = result.getFieldErrors();
            for (FieldError e : fieldErrors) {
                errormap.put(e.getField(), e.getDefaultMessage());
            }
            return Msg.fail().add("error", errormap);
        } else {
            empService.saveemp(emp);
            return Msg.success().add("emp", emp);
        }

    }


    /**
     * ajax验证姓名唯一性
     *
     * @param name
     * @return
     */
    @RequestMapping(value = "/checkname")
    @ResponseBody
    public Msg checkname(@RequestParam("name") String name) {
        String nameregex = "(^[a-zA-Z0-9_-]{3,16}$)|(^[\\u2E80-\\u9FFF]{2,5})";
        if (!name.matches(nameregex)) {
            return Msg.fail().add("va_name", "用户名必须6-16");
        }
        Emp emp = empService.findbyname(name);
        if (emp == null) {
            return Msg.success().add("va_name", "用户名必须6-16");
        } else {
            return Msg.fail().add("emps", emp);
        }
    }

    /**
     * 根据id查询员工
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/emps/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Msg findbyid(@PathVariable("id") Integer id) {
        Emp emp = empService.findbyid(id);
        return Msg.success().add("emp", emp);
    }

    /**
     * 更新员工
     *
     * @return
     */
    @RequestMapping(value = "/emps/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public Msg updateempbyid(Emp emp) {
        Integer nums = empService.updatebyid(emp);
        if (nums == 0) {
            return Msg.fail();
        } else {
            return Msg.success();
        }
    }

    /**
     * 根据id删除单个员工
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/emps/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public Msg delempbyid(Integer id) {
        Integer nums = empService.delempbyid(id);
        if (nums == 0) {
            return Msg.fail();
        } else {
            return Msg.success();
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/emps/{ids}", method = RequestMethod.POST)
    @ResponseBody
    public Msg delempall(@PathVariable("ids") String ids) {
        List<Integer> list = new ArrayList<>();
        String[] str = ids.split(",");
        for (String s : str) {
            list.add(Integer.parseInt(s));
        }
        Integer nums = empService.delempall(list);
        if (nums == 0) {
            return Msg.fail();
        } else {
            return Msg.success();
        }
    }

}
